package com.ruoyi.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.edu.mapper.EduVideoMapper;
import com.ruoyi.edu.domain.EduVideo;
import com.ruoyi.edu.service.IEduVideoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 课程视频Service业务层处理
 *
 * @author niruichen
 * @date 2020-12-22
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements IEduVideoService {

    @Override
    public List<EduVideo> queryList(EduVideo eduVideo) {
        LambdaQueryWrapper<EduVideo> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(eduVideo.getTitle())){
            lqw.eq(EduVideo::getTitle ,eduVideo.getTitle());
        }
        if (eduVideo.getIsFree() != null){
            lqw.eq(EduVideo::getIsFree ,eduVideo.getIsFree());
        }
        if (StringUtils.isNotBlank(eduVideo.getStatus())){
            lqw.eq(EduVideo::getStatus ,eduVideo.getStatus());
        }
        return this.list(lqw);
    }

    /**
     * 删除课程小节
     * @param courseId
     * TODO 需要完善，删除小结时要把里面的视频全部删除
     */
    @Override
    public void removeByCourseId(String courseId) {
        //先删除小结底下的视频
        //根据课程id查询课程的所有视频id
//        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
//        wrapperVideo.eq("course_id", courseId);
//        wrapperVideo.select("videoSourceId");
//        List<EduVideo> eduVideoList = baseMapper.selectList(wrapperVideo);
//        //将List<EduVideo>变成List<String>
//        List<String> videoIds = new ArrayList<>();
//        for (int i = 0; i < eduVideoList.size(); i++) {
//            String videoSourceId = eduVideoList.get(i).getVideoSourceId();
//            if(!StringUtils.isEmpty(videoSourceId)){
//                videoIds.add(videoSourceId);
//            }
//        }
//        if(videoIds.size() > 0){
//            //删除所有视频
//            vodClient.deleteBatch(videoIds);
//        }
        //删除课程小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
